package com.ctshk.rpc.order.ship.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.ship.dto.ShipConstants;
import com.ctshk.rpc.order.ship.entity.ShipManualOrder;
import com.ctshk.rpc.order.ship.entity.ShipOrder;
import com.ctshk.rpc.order.ship.entity.ShipOrderRoute;
import com.ctshk.rpc.order.ship.entity.ShipOrderTourist;
import com.ctshk.rpc.order.ship.mapper.ShipManualOrderMapper;
import com.ctshk.rpc.order.ship.mapper.ShipOrderMapper;
import com.ctshk.rpc.order.ship.mapper.ShipOrderRouteMapper;
import com.ctshk.rpc.order.ship.mapper.ShipOrderTouristMapper;
import com.ctshk.rpc.order.ship.req.ShipManualOrderReq;
import com.ctshk.rpc.order.ship.req.ShipOrderTouristReq;
import com.ctshk.rpc.order.ship.service.IShipManualOrderService;
import com.ctshk.rpc.ship.dto.ShipReserveListDTO;
import com.ctshk.rpc.ship.req.ShipProductManualReq;
import com.ctshk.rpc.ship.req.ShipProductSaleRecordReq;
import com.ctshk.rpc.ship.service.IShipProductSaleRecordService;
import com.ctshk.rpc.ship.service.IShipProductService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 船票手工单（外挂单） 服务实现类
 * </p>
 *
 * @author 刘蕾
 * @since 2021-03-16
 */
@DubboService
public class ShipManualOrderServiceImpl extends ServiceImpl<ShipManualOrderMapper, ShipManualOrder> implements IShipManualOrderService {


    @DubboReference
    private IShipProductService iShipProductService;
    @Autowired
    private ShipOrderMapper shipOrderMapper;
    @Autowired
    private ShipOrderTouristMapper shipOrderTouristMapper;

    @Autowired
    private ShipManualOrderMapper shipManualOrderMapper;

    @Autowired
    private ShipOrderRouteMapper shipOrderRouteMapper;

    @DubboReference
    private IShipProductSaleRecordService iShipProductSaleRecordService;

    @Override
    public Result addShipManualOrder(ShipManualOrderReq shipManualOrderReq, TokenUser user) {


        if (Objects.isNull(shipManualOrderReq)) {
            return Result.failed(SystemError.SHIP_PRODUCT_11025);
        }
        List<ShipOrderTouristReq> shipOrderTouristReqList = shipManualOrderReq.getShipOrderTouristReqList();
        if (CollectionUtils.isEmpty(shipOrderTouristReqList)) {
            return Result.failed(SystemError.SHIP_PRODUCT_11027);
        }

        ShipProductManualReq shipProductManualReq = new ShipProductManualReq();
        shipProductManualReq.setRouteId(shipManualOrderReq.getRouteId());
        shipProductManualReq.setVoyage(shipManualOrderReq.getVoyage());
        shipProductManualReq.setDepCityId(shipManualOrderReq.getDepCityId());
        shipProductManualReq.setArrCityId(shipManualOrderReq.getArrCityId());
        shipProductManualReq.setStartDate(shipManualOrderReq.getDepTime());
        shipProductManualReq.setCompanyType(2L);//珠江船票

        List<ShipReserveListDTO> shipReserveListDTOS = iShipProductService.queryManualList(shipProductManualReq);
        ShipReserveListDTO shipReserveListDTO = shipReserveListDTOS.get(0);


        //添加订单信息
        ShipOrder shipOrder = new ShipOrder();
        Long orderId = SnowflakeIdWorker.nextId();
        shipOrder.setId(orderId);
        shipOrder.setProductId(shipReserveListDTO.getId().toString());

        shipOrder.setGmtCreate(LocalDateTime.now());
        shipOrder.setCreateId(user.getId());
        shipOrder.setIsDeleted(ShipConstants.NOT_DELETE);

        //订单产品信息
        ShipOrderRoute shipOrderRoute = new ShipOrderRoute();
        shipOrderRoute.setId(SnowflakeIdWorker.nextId());
        shipOrderRoute.setProductId(shipReserveListDTO.getId());
        shipOrderRoute.setOrderId(orderId);

        shipOrderRoute.setCompanyType(Long.valueOf(shipReserveListDTO.getCompanyType()));
        shipOrderRoute.setDepCityId(Long.valueOf(shipReserveListDTO.getDepCityId()));
        shipOrderRoute.setArrCityId(Long.valueOf(shipReserveListDTO.getArrCityId()));
        shipOrderRoute.setDepCityName(shipReserveListDTO.getDepCityName());
        shipOrderRoute.setArrCityName(shipReserveListDTO.getArrCityName());
        shipOrderRoute.setDepPlaceId(shipReserveListDTO.getDepPlaceId());
        shipOrderRoute.setDepPlaceName(shipReserveListDTO.getDepPlaceName());
        shipOrderRoute.setArrPlaceId(shipReserveListDTO.getArrPlaceId());
        shipOrderRoute.setArrPlaceName(shipReserveListDTO.getArrPlaceName());
        shipOrderRoute.setVoyage(shipReserveListDTO.getVoyage());
        shipOrderRoute.setCabinClass(shipReserveListDTO.getCabinClass());
        shipOrderRoute.setNumber(shipManualOrderReq.getNumber());
        shipOrderRoute.setGmtCreate(LocalDateTime.now());
        shipOrderRoute.setCreateId(user.getId());
        shipOrderRoute.setIsDeleted(ShipConstants.NOT_DELETE);
        shipOrderRouteMapper.insert(shipOrderRoute);

        //销售编号
        //shipOrder.setOrderNumber(buildProductNumber(user.getId(), LocalDateTime.now()));

        // 计算付款到期时间,+30分钟
        shipOrder.setPayExpireDate(LocalDateTime.now().plusMinutes(30));
        //单服务费*面票数
        shipOrder.setServiceFee(shipManualOrderReq.getServiceAmount());
        //订单总额
        shipOrder.setOrderAmount(shipManualOrderReq.getExpendAmount());
        shipOrder.setScheduledStatus(1); //已预订
        shipOrder.setPaymentStatus(0); //未付款
        shipOrder.setCancelStatus(0); //未取消
        shipOrder.setSalesmanId(user.getId());
        shipOrder.setSalesmanName(user.getUsername());
        shipOrder.setSalesMethods(1);//销售方式(1：单品销售 2：打包销售)
        shipOrder.setProductType(2);//产品类型(1手工单，2外挂单)
        shipOrder.setCreateId(user.getId());

        shipOrderMapper.insert(shipOrder);


        //组装产品库存售卖记录body
        ShipProductSaleRecordReq shipProductSaleRecordReq = new ShipProductSaleRecordReq();
        shipProductSaleRecordReq.setId(SnowflakeIdWorker.nextId());
        shipProductSaleRecordReq.setOrderId(orderId);
        shipProductSaleRecordReq.setProductId(shipReserveListDTO.getId());
        shipProductSaleRecordReq.setSalesDate(shipManualOrderReq.getDepTime());
        shipProductSaleRecordReq.setSoldNumber(shipManualOrderReq.getNumber());
        //1:自营 2:代理商 3:官方渠道 4:第三方
        shipProductSaleRecordReq.setChannel(1);
        //1 线上 2线下
        shipProductSaleRecordReq.setLine(2);
//        shipProductSaleRecordReq.setCheckType(user.getId());
        shipProductSaleRecordReq.setCustomId(user.getId());
        shipProductSaleRecordReq.setCustomName(user.getUsername());
        iShipProductSaleRecordService.insertRecord(shipProductSaleRecordReq, user);


        //添加外挂订单信息
        ShipManualOrder shipManualOrder = new ShipManualOrder();
        BeanUtils.copyProperties(shipManualOrderReq, shipManualOrder);
        shipManualOrder.setId(SnowflakeIdWorker.nextId());
        shipManualOrder.setOrderId(orderId);
        shipManualOrderMapper.insert(shipManualOrder);

        //添加订单旅客信息
        for (ShipOrderTouristReq shipOrderTouristReq : shipOrderTouristReqList) {
            ShipOrderTourist shipOrderTourist = new ShipOrderTourist();
            shipOrderTourist.setId(SnowflakeIdWorker.nextId());
            shipOrderTourist.setOrderId(orderId);
            shipOrderTourist.setCreateId(user.getId());
            shipOrderTourist.setGmtCreate(LocalDateTime.now());
            shipOrderTouristMapper.insert(shipOrderTourist);
        }

        return null;
    }
}
